<?php
class AuthController extends FrontController
{
	public function indexAction()
	{
		$auth = CF_Auth::getInstance();
		if($auth->has()) $this->getResponse()->getHeader()->redirect('index');
		$this->getView()->setRenderScript(true);
	}
	
	public function loginAction()
	{
		$auth = CF_Auth::getInstance();
		if($auth->has()) $this->getResponse()->getHeader()->redirect('index');
		
		$email = $this->getRequest()->email;
		$password = $this->getRequest()->password;
		$langId = $this->getRequest()->lang_id;
		
		$authAdapter = new CF_Auth_Adapter_Db();
		$authAdapter->getDbSelect()
			->table('##_users')
			->where(new CF_Db_Expr('`email`=%s AND password=MD5(%s)', $email, $password));
		
		$auth->authenticate($authAdapter);
		
		$success = false;
		$msg = '';
		if($auth->has()){
			$storage = $auth->getStorage();
			$data = $storage->getData();
			$active = $data->active;
			
			switch($active){
				case -1:
					$msg = 'Your account has been locked';
					$auth->getStorage()->clear();
					break;
					
				case 0:
					$msg = 'Your account is not active';
					$auth->getStorage()->clear();
					break;
					
				case 1:
					$success = true;
					$msg = 'Login successful';
					CF_Session::set('langId', $langId);
					break;
					
				default:
					$msg = 'Login failure';
					
			}
		}else{
			$msg = 'The email or password you entered is incorrect';
		}
		return array('success' => $success, 'msg' => $msg);
	}
	
	/**
	 * @len 0
	 */
	public function logoutAction()
	{
		$auth = CF_Auth::getInstance();
		$auth->getStorage()->clear();
		
		return array('success' => true);
	}
}